package com.leapp.sk.viewdelegate.archives

import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.GlideCircleTransform
import com.example.mvp.view.SKTopBar
import com.example.mvp.view.WindowBottomDialog
import com.leapp.sk.R
import com.leapp.sk.ui.activity.archives.CompleteBabyInfoActivity
import com.leapp.sk.ui.activity.login.PerfectUserInfoActivity

/**
 * 完善宝贝信息视图托管
 * @author  chaohaipeng  on 2018/8/7.
 */
class CompleteBabyInfoDelegate: BaseAppDelegate() {
    /**
     * 图片加载管理
     */
    var mRequestManager: RequestManager? = null

    /**
     * 图片加载操作
     */
    var mRequestOptions: RequestOptions = RequestOptions()


    /**
     * 底部弹出Dialog
     */
    private var windowBottomDialog: WindowBottomDialog? = null
    /**
     * 底部弹出View
     */
    private var bottomView: View? = null

    /**
     * 第一个选项文本
     */
    private var tv_dailog_bottom_frist_text: TextView? = null
    /**
     * 第二个选项文本
     */
    private var tv_dailog_bottom_second_text: TextView? = null
    /**
     * 取消选项文本
     */
    private var tv_dailog_bottom_cancel_text: TextView? = null

    /**
     * 标题
     */
    private var tv_dailog_bottom_title_text: TextView? = null

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_complete_baby_info
    }

    /**
     * 初始化底部弹出
     */
    fun initBottomDialog() {
        if (windowBottomDialog == null) {
            windowBottomDialog = WindowBottomDialog(getActivity(), 0)
        }
    }

    /**
     * 初始化底部弹出视图
     */
    fun initBottomDialogView() {
        if (bottomView == null) {
            bottomView = View.inflate(getActivity(), R.layout.layout_bottom_dialog, null)
            tv_dailog_bottom_frist_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_frist_text)
            tv_dailog_bottom_second_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_second_text)
            tv_dailog_bottom_cancel_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_cancel_text)
            tv_dailog_bottom_title_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_title_text)

        }
    }

    /**
     * 设置头像
     */
    fun setHeadData(path: String?) {
        val iv_acbi_head = getView<ImageView>(R.id.iv_acbi_head)
        mRequestManager = Glide.with(getActivity<AppCompatActivity>())
        mRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
        mRequestOptions.skipMemoryCache(true)
        mRequestOptions.error(R.mipmap.icon_defout_head)
        mRequestOptions.transforms(GlideCircleTransform(getActivity()))
        mRequestManager?.load(path)
                ?.thumbnail(0.2f)
                ?.apply(mRequestOptions)
                ?.into(iv_acbi_head)
    }

    /**
     * 初始化性别底部弹出窗
     */
    fun initSexDialog() {
        initBottomDialog()
        initBottomDialogView()
        tv_dailog_bottom_frist_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_male)
        tv_dailog_bottom_second_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_female)
        tv_dailog_bottom_title_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_sex_title)
        windowBottomDialog?.setBottomView(bottomView)
        windowBottomDialog?.show()
        initEvent(true)
    }


    /**
     * 初始化事件
     */
    fun initEvent(isSex: Boolean) {
        initBottomDialog()
        initBottomDialogView()
        tv_dailog_bottom_frist_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
                setSexData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_male))
        })
        tv_dailog_bottom_second_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
                setSexData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_female))
        })
        tv_dailog_bottom_cancel_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
        })
    }


    /**
     * 宝贝姓名校验和获取
     */
    fun nameCheck():String {
        val et_acbi_name = getView<ContainsEmojiEditText>(R.id.et_acbi_name)
        val trim = et_acbi_name.text.toString().trim()
        if (!TextUtils.isEmpty(trim)){
            return trim
        }else{
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_info_hint_name))
            return ""
        }
    }
    /**
     * 性别校验和获取
     */
    fun sexCheck():String {
        val tv_acbi_sex = getView<TextView>(R.id.tv_acbi_sex)
        val trim = tv_acbi_sex.text.toString().trim()
        if (!TextUtils.isEmpty(trim)){
            return trim
        }else{
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_info_toast_sex))
            return ""
        }
    }
    /**
     *出生日期校验和获取
     */
    fun birthCheck():String {
        val tv_acbi_birth_date = getView<TextView>(R.id.tv_acbi_birth_date)
        val trim = tv_acbi_birth_date.text.toString().trim()
        if (!TextUtils.isEmpty(trim)){
            return trim
        }else{
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_info_toast_birth_date))
            return ""
        }
    }

    /**
     * 设置性别数据
     */
    private fun setSexData(content: String?) {
        val tv_acbi_sex = getView<TextView>(R.id.tv_acbi_sex)
        if (!TextUtils.isEmpty(content)) {
            tv_acbi_sex.text = content
        }
    }



    /**
     * 设置时间数据
     */
    fun setTimeData(content: String?) {
        val tv_acbi_birth_date = getView<TextView>(R.id.tv_acbi_birth_date)
        if (!TextUtils.isEmpty(content)) {
            tv_acbi_birth_date.text = content
        }
    }
    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_apui= getView<SKTopBar>(R.id.jtb_acbi)
        if (!TextUtils.isEmpty(title)){
            jtb_apui.setTitle(title)
        }
        setEvent(jtb_apui)
    }

    /**
     * 设置设置事件
     */
    fun setEvent(topBar:SKTopBar){
        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<CompleteBabyInfoActivity>().finish()

            }
            override fun rightOnClick() {
            }

        })
    }

}